home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #1 / Amiga Plus CD - 2000 - No. 1.iso / Tools / Dev / mamesrc / fixsrc / fixsrc.c < prev    next >
C/C++ Source or Header  |  1999-12-03  |  2KB  |  139 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4.  
  5. #include <exec/types.h>
  6. #include <exec/execbase.h>
  7. #include <dos/dos.h>
  8.  
  9. #include <inline/exec.h>
  10. #include <inline/dos.h>
  11.  
  12. #include "scandir.h"
  13.  
  14. extern struct ExecBase    *SysBase;
  15.  
  16. struct DosLibrary        *DOSBase    = NULL;
  17.  
  18. BOOL FileFunc(char *name, struct FileInfoBlock *fib)
  19. {
  20.     static char include[]="include";
  21.     BPTR        file;
  22.     LONG        size;
  23.     char        *buffer;
  24.     int            rename;
  25.     int            i, j, k;
  26.  
  27.     if(SetSignal(0, 0) & SIGBREAKF_CTRL_C)
  28.         return(FALSE);
  29.     
  30.     rename = FALSE;
  31.     
  32.     for(i = 0; name[i]; i++)
  33.     {
  34.         if(isupper(name[i]))
  35.         {
  36.             name[i]    = tolower(name[i]);
  37.             rename    = TRUE;
  38.         }
  39.     }
  40.  
  41.     if(fib->fib_DirEntryType < 0)
  42.     {
  43.         size = fib->fib_Size;
  44.     
  45.         buffer = malloc(size);
  46.         
  47.         if(buffer)
  48.         {
  49.             file = Open(name, MODE_OLDFILE);
  50.             
  51.             if(file)
  52.             {
  53.                 if(Read(file, buffer, size) == size)
  54.                 {
  55.                     SetFileSize(file, 0, OFFSET_BEGINNING);
  56.                     Seek(file, 0, OFFSET_BEGINNING);
  57.                 
  58.                     for(i = 0, j = 0; j < size; j++)
  59.                     {
  60.                         if(buffer[j] == 13)
  61.                         {
  62.                             if(i < j)
  63.                                 Write(file, &buffer[i], j - i);
  64.  
  65.                             i = j + 1;
  66.                         }
  67.                         else if(buffer[j] == '#')
  68.                         {
  69.                             j++;
  70.                             
  71.                             for(k = 0; (k < 7) && (j < size) && (include[k] == tolower(buffer[j])); j++, k++);
  72.  
  73.                             if(k == 7)
  74.                             {
  75.                                 for(; (j < size) && ((buffer[j] == ' ') || (buffer[j] == '\t')); j++);
  76.     
  77.                                 if(j < size)
  78.                                 {
  79.                                     if((buffer[j] == '<') || (buffer[j] == '"'))
  80.                                     {
  81.                                         j++;
  82.                                         
  83.                                         for(; (j < size) && (buffer[j] != '>') && (buffer[j] != '"'); j++)
  84.                                         {
  85.                                             if(buffer[j] == '\\')
  86.                                                 buffer[j] = '/';
  87.                                             else
  88.                                                 buffer[j] = tolower(buffer[j]);
  89.                                         }
  90.                                     }
  91.                                     else
  92.                                         j--;
  93.                                 }
  94.                             }
  95.                             else
  96.                                 j--;
  97.                         }
  98.                     }
  99.                     
  100.                     if(i < j)
  101.                         Write(file, &buffer[i], j - i);
  102.                     
  103.                     Close(file);
  104.                 }
  105.                 else
  106.                     Close(file);
  107.             }
  108.             
  109.             free(buffer);
  110.         }
  111.     }
  112.  
  113.     if(rename)
  114.         Rename(name,name);
  115.     
  116.     return(TRUE);
  117. }
  118.  
  119. int main(int argc, char **argv)
  120. {
  121.     char *dir;
  122.     int    rc;
  123.  
  124.     if((DOSBase = (struct DosLibrary *) OpenLibrary("dos.library", 37)))
  125.     {
  126.         if(argc > 1)
  127.             dir = argv[1];
  128.         else
  129.             dir = NULL;
  130.         
  131.         rc = ScanDir(dir, FileFunc, SDF_DirCall|SDF_FilePattern|SDF_FileCall|SDF_Recursive, "(#?.c|#?.h|#?.txt)");
  132.             
  133.         if(rc)
  134.             printf("Error: %d\n", rc);
  135.     }
  136.  
  137.     return(0);
  138. }
  139.